package view.plugin
{
	//加油界面
	
	import event.GlobalEvent;
	
	import flash.display.MovieClip;
	import flash.display.SimpleButton;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	
	import flash3.framework.Singleton;
	
	import global.Resource;
	
	import model.*;
	
	import vo.SupplyItem;
	
	public class LackPetrolPanel extends PanelBase
	{
		private var havePetrol:TextField = null;
		private var needPetrol:TextField = null;
		private var item:Array = [];
		private var btnExit:SimpleButton = null;
		private var dlg:ExitPanel = null;
		private var isBusy:Boolean = false;
		public static const EXIT:String = "exit";
		public static const SELECT_ITEM:String = "select_item";
		
		public function LackPetrolPanel()
		{
			super();
			var main:MovieClip = Resource.i.getMovieClip("dlgLackPetrol", "DlgLackPetrol");
			addChild(main);
			btnExit = main.btnExit;
			havePetrol = main.havePetrol;
			needPetrol = main.needPetrol;
			item.push(main.item1,main.item2,main.item3,main.item4);
			btnExit.addEventListener(MouseEvent.CLICK, onClickBtnExit);
			for(var i:uint = 0; i < item.length; i++)
			{
				item[i].petrol.mouseEnabled = false;
				item[i].gold.mouseEnabled = false;
				item[i].btn.addEventListener(MouseEvent.CLICK, onClickItem);
			}
		}
		
		override public function show(v:Boolean):void
		{
			super.show(v);
			if(v)showDlg(false);
		}
		private function onClickBtnExit(evt:MouseEvent):void
		{
			showDlg(true);
		}
		public function set busy(b:Boolean):void
		{
			isBusy = b;
		}
		private function showDlg(v:Boolean):void
		{
			btnExit.mouseEnabled = !v;
			dlg = ExitPanel.i;
			for(var i:uint = 0; i < item.length; i++)
			{
				item[i].btn.mouseEnabled = !v;
			}
			if(v)
			{
				dlg.text = ExitPanel.getStr1();
				dlg.addEventListener(ExitPanel.OK, onClickDlgBtnOK);
				dlg.addEventListener(ExitPanel.CANSEL, onClickDlgBtnCansel);
				var sprite:Sprite = ChessLobby.dialog;
				sprite.addChild(dlg);
			}
			else
			{
				if(dlg.parent)
					dlg.parent.removeChild(dlg);
				dlg.removeEventListener(ExitPanel.OK, onClickDlgBtnOK);
				dlg.removeEventListener(ExitPanel.CANSEL, onClickDlgBtnCansel);
			}
		}
		private function onClickDlgBtnOK(evt:Event):void
		{
			showDlg(false);
			this.show(false);
			this.dispatchEvent(new Event(EXIT));
		}
		private function onClickDlgBtnCansel(evt:Event):void
		{
			showDlg(false);
		}
		private function onClickItem(evt:MouseEvent):void
		{
			if(isBusy) return;
			for(var i:uint = 0; i < item.length; i++)
			{
				if(item[i].btn == evt.target)
				{
					isBusy = true;
					var globalEvent:GlobalEvent = new GlobalEvent(SELECT_ITEM);
					globalEvent.data = i;
					dispatchEvent(globalEvent);
					break;
				}
			}
		}
		
		public function updateData():void
		{
			havePetrol.text = MeInfoModel.i.userScore.lScore.toString();
			needPetrol.text = GameModel.i.serverRunInfo.minMoney.toString();
			var itemCount:uint = SupplyItemModel.i.length;
			for(var i:uint = 0; i < itemCount; i++)
			{
				var supplyItem:SupplyItem = SupplyItemModel.i.value(i);
				item[i].petrol.text = supplyItem.money;
				item[i].gold.text = supplyItem.gold;
			}
		}
		
		static public function get i():LackPetrolPanel
		{
			return Singleton.getInstanceOrCreate(LackPetrolPanel) as LackPetrolPanel;
		}
	}
}